﻿using System;
using System.Collections.Generic;
using System.Text;
using Gammar.Digger.DALFactory;
using Gammar.Digger.Entity;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.IDAL;
namespace Gammar.Digger.BLL
{
    public class PluginManagement
    {
        static Idigger_plugin plugin;
        static Iplugin_run_status run_status;
        static Iplugin_run_log run_log;
        //static Iplugin_run_log pluginLog;
        //static Iplugin_run_status pluginRunStatus;
        //static Iweb_site website;

      

      
        static PluginManagement()
        {
            //plugin= DataAccess.Createdigger_plugin();
            //pluginLog = DataAccess.Createplugin_run_log();
            //pluginRunStatus = DataAccess.Createplugin_run_status();
            //website = DataAccess.Createweb_site();
        }

        /// <summary>
        /// 添加新插件到数据库
        /// </summary>
        /// <param name="plugin"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string Add(digger_pluginEntity diggerPlugin)
        {
            try
            {
                plugin = DataAccess.Createdigger_plugin();
                plugin.Add(diggerPlugin);
                return Messages.Success;
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return Messages.Failed;
            }
           
        }

        public static string Update(digger_pluginEntity diggerPlugin)
        {
            try
            {
                plugin = DataAccess.Createdigger_plugin();
                plugin.Update(diggerPlugin);
                return Messages.UpdateSuccess;
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return Messages.Failed;
            }

        }

        /// <summary>
        /// 更新插件状态
        /// </summary>
        /// <param name="pluginId"></param>
        /// <param name="isValid"></param>
        /// <returns></returns>
        public static bool UpdatePluginStatus(string pluginId,bool isValid)
        {
            try
            {
                plugin = DataAccess.Createdigger_plugin();
                digger_pluginEntity p = new digger_pluginEntity();

                p.is_valid = isValid;

                int reval= plugin.Updatepartial(p, "plugin_id=" + pluginId);

                return reval == 1 ? true : false;
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return false;
            }
        }



        public static SortList<digger_pluginEntity> GetList(string where)
        {
            try
            {
                plugin = DataAccess.Createdigger_plugin();

                return plugin.GetList(where);
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 根据插件所属
        /// </summary>
        /// <param name="websiteId"></param>
        /// <returns></returns>
        private static string LoadPluginSchema(string websiteId)
        {
            return null;
        }

        public static digger_pluginEntity GetPluginById(string pluginId)
        {
            try
            {
                plugin = DataAccess.Createdigger_plugin();
                return plugin.GetEntity(int.Parse(pluginId));
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return null;
            }
        }

        public static SortList<plugin_run_statusEntity> GetRunPluginStatus(int sysId)
        {
            try
            {
                run_status = DataAccess.Createplugin_run_status();
                return run_status.GetList("sys_id="+sysId.ToString());
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return null;
            }
        }

        public static SortList<plugin_run_logEntity> GetRunPluginLogs(int statusid)
        {
            try
            {
                run_log = DataAccess.Createplugin_run_log();
                return run_log.GetList("status_id=" + statusid.ToString());
            }
            catch (Exception ex)
            {
                LogUtility.WriteErrLog(ex.Message);
                return null;
            }
        }



    }
}
